<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="form"
        label-width="77px"
        class="ele-form-search"
        @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="请求接口:">
              <el-select size="mini"
                         @change="setContent"
                         clearable
                         v-model="form.kdlcode"
                         placeholder="请选择板块"
                         class="ele-fluid">
                <el-option v-for="item in codeList" :label="item.name" :value="item.code" :key="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <div class="ele-form-actions" style="padding-top: 4px">
              <el-button
                type="primary"
                icon="el-icon-download"
                class="ele-btn-icon"
                size="mini"
                @click="reload">获取
              </el-button>
              <el-button @click="reset" size="mini">重置</el-button>
            </div>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :lg="24" :md="24">
            <el-form-item label="请求报文:">
              <el-input type="textarea" :rows="20" v-model="form.requestBody"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="15">
        </el-row>
      </el-form>
      <!-- 数据表格 -->
    </el-card>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import setting from "@/config/setting";
export default {
  name: 'KdlAPi',
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/c37infrkscbillnew/list',
      form:{
        kdlcode:'',
        requestBody:'',
      },
      // 表格选中数据
      selection: [],
      codeList:[
        {code:'Z01-KDL',name:'采购质检(Z01-KDL)'},
        {code:'ZST3',name:'采退出库(ZST3)'},
        {code:'WE',name:'采购收货(WE)'},
        {code:'Z04-KDL',name:'销售出库(Z04-KDL)'},
        {code:'Z05-KDL',name:'销退入库(Z05-KDL)'},
        {code:'RGR',name:'拒收订单(RGR)'},
        {code:'GM',name:'移库结果(GM)'},
        {code:'QFF',name:'异常反馈(QFF)'},
        {code:'POD',name:'签收(POD)'},
        {code:'Inventory',name:'实时库存(Inventory)'},
        {code:'INST',name:'养护记录(INST)'},
        {code:'ZTRN',name:'运输记录(ZTRN)'},
        {code:'MARA',name:'主数据审批(MARA)'},
      ],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showLog: false,
      showDetail:false,
    };
  },
  methods: {
    setContent(){
      if(this.form.kdlcode=='Z01-KDL' || this.form.kdlcode=='RGR' || this.form.kdlcode=='QFF' || this.form.kdlcode=='WE' || this.form.kdlcode=='POD'  || this.form.kdlcode=='GM' || this.form.kdlcode=='ZST3' || this.form.kdlcode=='Z04-KDL' || this.form.kdlcode=='Z05-KDL')
      {
        this.form.requestBody='<?xml version="1.0" encoding="UTF-8"?>\n' +
          '<CAH3PL>\n' +
          '    <Head>\n' +
          '        <PrincipalID>'+setting.companyid+'</PrincipalID>\n' +
          '        <DocType>'+this.form.kdlcode+'</DocType>\n' +
          '        <StartTime>'+this.$util.toDateString(setting.getFromTime(),'yyyy-MM-dd HH:mm:ss')+'</StartTime>\n' +
          '        <EndTime>'+this.$util.toDateString(new Date(),'yyyy-MM-dd HH:mm:ss')+'</EndTime>\n' +
          '    </Head>\n' +
          '</CAH3PL> '
      }else if(this.form.kdlcode=='MARA')
      {
        this.form.requestBody='<?xml version="1.0" encoding="UTF-8"?>\n' +
          '<CAH3PL>\n' +
          '    <Head>\n' +
          '        <PrincipalID>'+setting.companyid+'</PrincipalID>\n' +
          '        <DocType>'+this.form.kdlcode+'</DocType>\n' +
          '        <MasterType>M</MasterType>\n' +
          '        <StartTime>'+this.$util.toDateString(setting.getFromTime(),'yyyy-MM-dd HH:mm:ss')+'</StartTime>\n' +
          '        <EndTime>'+this.$util.toDateString(new Date(),'yyyy-MM-dd HH:mm:ss')+'</EndTime>\n' +
          '    </Head>\n' +
          '</CAH3PL> '
      }else if(this.form.kdlcode=='INST')
      {
        this.form.requestBody='<?xml version="1.0" encoding="UTF-8"?>\n' +
          '<CAH3PL>\n' +
          '    <Head>\n' +
          '        <PrincipalID>'+setting.companyid+'</PrincipalID>\n' +
          '        <DocType>INST</DocType>\n' +
          '        <FROMDATE>'+this.$util.toDateString(new Date(),'yyyyMMdd')+'</FROMDATE>\n' +
          '        <TODATE>'+this.$util.toDateString(new Date(),'yyyyMMdd')+'</TODATE>\n' +
          '    </Head>\n' +
          '</CAH3PL>'
      }else if(this.form.kdlcode=='ZTRN')
      {
        this.form.requestBody='<?xml version="1.0" encoding="UTF-8"?>\n' +
          '<CAH3PL>\n' +
          '    <Head>\n' +
          '        <PrincipalID>'+setting.companyid+'</PrincipalID>\n' +
          '        <OrderNo></OrderNo>\n' +
          '        <DocType>ZTRN</DocType>\n' +
          '    </Head>\n' +
          '    <Details>\n' +
          '        <Detail>\n' +
          '            <Material></Material>\n' +
          '            <BATCH></BATCH>\n' +
          '            <FROMDATE>'+this.$util.toDateString(new Date(),'yyyyMMdd')+'</FROMDATE>\n' +
          '            <TODATE>'+this.$util.toDateString(new Date(),'yyyyMMdd')+'</TODATE>\n' +
          '            <POINT></POINT>\n' +
          '            <DELIVERY></DELIVERY>\n' +
          '        </Detail>\n' +
          '    </Details>\n' +
          '</CAH3PL>'
      }else if(this.form.kdlcode=='Inventory')
      {
        this.form.requestBody='<?xml version="1.0" encoding="UTF-8"?>\n' +
          '<CAH3PL>\n' +
          '    <Head>\n' +
          '        <PrincipalID>'+setting.companyid+'</PrincipalID>\n' +
          '        <DocType>Inventory</DocType>\n' +
          '        <Plant>T150</Plant>\n' +
          '    </Head>\n' +
          '    <Details>\n' +
          '        <Detail>\n' +
          '            <Material></Material>\n' +
          '            <VenderBatch></VenderBatch>\n' +
          '        </Detail>\n' +
          '    </Details>\n' +
          '</CAH3PL>'
      }
    },
    getFromDate() {
      const timeOne = new Date()
      const year = timeOne.getFullYear()
      let month = timeOne.getMonth()
      let day = 1
      //month = month < 10 ? '0' + month : month
      //day = day < 10 ? '0' + day : day
      ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
      return new Date(year,month,day);
    },

    /* 刷新表格 */
    reload() {
      if(!this.form.kdlcode){
        this.$message.info('请选择康德乐接口');
        return;
      }
      this.$confirm('确定要手动获取康德乐接口数据吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/kdl/manualsync',this.form).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            //this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
      //this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.form = {};
      //this.reload();
    },

    openLog(row){
      console.log(row);
      this.current = row;
      this.showLog = true;
    },

    openDetail(row){
      console.log(row);
      this.current = row;
      this.showDetail = true;
    },
    /* 显示编辑 */
    openEdit(row) {
      // if (!this.selection.length) {
      //   this.$message.error('请至少选择一条数据');
      //   return;
      // }
      this.$confirm('确定要将选中的采购入库记录回写至NC吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/esbservice/cgrk/'+ [row.rukNo]).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            //this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/c37infrkscbillnew/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的采购入库/销售退回上传单表吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/c37infrkscbillnew/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
.ele-form-search{
  margin-top: -15px ;
  margin-bottom: -5px;
}
.el-form-item {
  margin-bottom: -5px !important;
}
</style>
